package action.meetRecord;

import java.util.List;

import model.MeetRecord;
import model.MeetRecordMessage;
import model.Page;
import model.User;

import DAO.MeetRecordDAO;
import DAO.UserDAO;
import DAO.DAOImple.MeetRecordImple;
import DAO.DAOImple.UserDAOImple;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MeetRecordAction extends ActionSupport {

	private MeetRecord meetRecord;
	private List<MeetRecord> meetRecords;
	private List<MeetRecordMessage> mRMessages;
	private MeetRecordMessage meetRecordMessage;

	private Page page;
	private String querySQL;

	public String execute() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		if (querySQL == null || querySQL.trim().equals("")) {
			querySQL = this.querySQL(meetRecord, "m");
		}

		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		String pageSql = (querySQL.trim().equals("")) ? ("where state = 0"): "as m where " + querySQL.substring(5, querySQL.length())
						+ " and m.state = 0";
		page = page.getPage("T_MeetingRecord " + pageSql, page.getCurrentPage(), 50);

		meetRecords = meetRecordDao.getMeetRecodList(querySQL, page);

		return SUCCESS;
	}
	
	/*
	 * 通过id返回例会记录
	 */
	public String getMeetRecordByID() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		meetRecord = meetRecordDao.getMeetRecord(meetRecord.getId()); // 获得例会详细信息

		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_MeetRecordMessage where meetRecordId = "
				+ meetRecord.getId(), page.getCurrentPage(), 20); // 获得分页对象

		mRMessages = meetRecordDao.getMRMessageList(meetRecord.getId(), page); // 获得例会留言

		return SUCCESS;
	}

	/*
	 * 通过id返回例会记录修改
	 */
	public String getMeetRecordForM() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		meetRecord = meetRecordDao.getMeetRecord(meetRecord.getId()); // 获得例会详细信息

		return SUCCESS;
	}

	/*
	 * 增加例会留言
	 */
	public String addMRMessage() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		int result = meetRecordDao.addMRMessage(meetRecordMessage);
		if (result == 0) {
			//System.out.println("例会留言失败");
		} else {
			//System.out.println("例会留言成功");
		}

		meetRecord = meetRecordDao.getMeetRecord(meetRecordMessage.getMeetRecord().getId()); // 获得例会详细信息

		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_MeetRecordMessage where meetRecordId = "
				+ meetRecord.getId(), page.getCurrentPage(), 20); // 获得分页对象

		mRMessages = meetRecordDao.getMRMessageList(meetRecord.getId(), page); // 获得例会留言
		return SUCCESS;
	}

	/*
	 * 删除例会记录留言
	 */
	public String deleteMRMessage() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		int result = meetRecordDao.deleteMRMessage(meetRecordMessage.getId());
		if (result == 0) {
			//System.out.println("删除例会留言失败");
		} else {
		//	System.out.println("删除例会留言成功");
		}

		meetRecord = meetRecordDao.getMeetRecord(meetRecordMessage
				.getMeetRecord().getId()); // 获得例会详细信息

		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_MeetRecordMessage where meetRecordId = "
				+ meetRecord.getId(), page.getCurrentPage(), 20); // 获得分页对象

		mRMessages = meetRecordDao.getMRMessageList(meetRecordMessage
				.getMeetRecord().getId(), page); // 获得例会留言
		return SUCCESS;

	}

	/*
	 * 增加例会记录
	 */
	public String addMeetRecord() {

		ActionContext act = ActionContext.getContext();

		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		UserDAO userDao = new UserDAOImple(); // 首先检查填写的主持人和记录人是否正确存在
		User organizer = userDao.getUser(meetRecord.getOrganizer().getName());
		User recorder = userDao.getUser(meetRecord.getRecorder().getName());
		if (organizer == null) {
			act.put("addwrong", 0); // 所填写的主持人名字错误 或不存在数据库中
			return ERROR;
		} else if (recorder == null) {
			act.put("addwrong", 1); // 所填写的记录人名字错误 或不存在数据库中
			return ERROR;
		} else {
			meetRecord.setOrganizer(organizer);
			meetRecord.setRecorder(recorder);

			int addResult = meetRecordDao.addMeetRecord(meetRecord);
			if (addResult == 0) {
				//System.out.println("失败");
				act.put("addwrong", 3);
				return ERROR;
			} else {
				//System.out.println("成功");
				if (page == null) {
					page = new Page();
					page.setCurrentPage(1);
				}
				meetRecord.changeZ();
				page = page.getPage("T_MeetingRecord where state = 0", page
						.getCurrentPage(), 50);
				meetRecords = meetRecordDao.getMeetRecodList("", page);
			}
			return SUCCESS;
		}
	}

	/*
	 * 修改例会记录
	 */
	public String modifyMeetRecord() {
		ActionContext act = ActionContext.getContext();

		MeetRecordDAO meetRecordDao = new MeetRecordImple();
		UserDAO userDao = new UserDAOImple(); // 首先检查填写的主持人和记录人是否正确存在
		User organizer = userDao.getUser(meetRecord.getOrganizer().getName());
		User recorder = userDao.getUser(meetRecord.getRecorder().getName());
		if (organizer == null) {
			act.put("wrong", 0); // 所填写的主持人名字错误 或不存在数据库中
			return ERROR;
		} else if (recorder == null) {
			act.put("wrong", 1); // 所填写的记录人名字错误 或不存在数据库中
			return ERROR;
		} else {
			meetRecord.setOrganizer(organizer);
			meetRecord.setRecorder(recorder);
			int result = meetRecordDao.modifyMeetRecord(meetRecord);
			if (result == 0) {  //失败
				meetRecord = meetRecordDao.getMeetRecord(meetRecord.getId()); // 获得例会详细信息
				return ERROR;
			} else {    //成功
				meetRecord = meetRecordDao.getMeetRecord(meetRecord.getId()); // 获得例会详细信息

				if (page == null) {
					page = new Page();
					page.setCurrentPage(1);
				}
				page = page.getPage("T_MeetRecordMessage where meetRecordId = "
						+ meetRecord.getId(), page.getCurrentPage(), 50); // 获得分页对象

				mRMessages = meetRecordDao.getMRMessageList(meetRecord.getId(),
						page); // 获得例会留言
			}
		}
		return SUCCESS;
	}

	/*
	 * 删除会议记录 并不真正删除 把state改为1；
	 */
	public String deleteMeetRecord() {
		MeetRecordDAO meetRecordDao = new MeetRecordImple();

		int result = meetRecordDao.deleteMeetRecord(meetRecord.getId());
		if (result == 0) {
			//System.out.println("失败");
		} else {
			//System.out.println("成功");
		}

		if (page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_MeetingRecord where state = 0", page
				.getCurrentPage(), 50);
		meetRecords = meetRecordDao.getMeetRecodList("", page);

		return SUCCESS;
	}


	public String querySQL(MeetRecord meetRecord, String tableName) {
		StringBuilder querySql = new StringBuilder();
		if (meetRecord != null) {
			int groupId = meetRecord.getGroup().getId();
			int schoolYear = meetRecord.getSchoolYear();
			int semester = meetRecord.getSemester();
			int weekNum = meetRecord.getWeekNum();

			if (groupId != 0) {
				querySql.append(" and " + tableName + "." + "groupId = "
						+ groupId);
			}
			if (schoolYear != 0) {
				querySql.append(" and " + tableName + "." + "schoolYear = "
						+ schoolYear);
			}
			if (semester != 0) {
				querySql.append(" and " + tableName + "." + "semester = "
						+ semester);
			}
			if (weekNum != 0) {
				querySql.append(" and " + tableName + "." + "weekNum = "
						+ weekNum);
			}
			// if(!(querySql.toString().equals(""))) {
			// querySql.insert(querySql.length(), "and ");
			// }
		}
		return querySql.toString();

	}

	public MeetRecord getMeetRecord() {
		return meetRecord;
	}

	public void setMeetRecord(MeetRecord meetRecord) {
		this.meetRecord = meetRecord;
	}

	public List<MeetRecord> getMeetRecords() {
		return meetRecords;
	}

	public void setMeetRecords(List<MeetRecord> meetRecords) {
		this.meetRecords = meetRecords;
	}

	public List<MeetRecordMessage> getMRMessages() {
		return mRMessages;
	}

	public void setMRMessages(List<MeetRecordMessage> mRMessages) {
		this.mRMessages = mRMessages;
	}

	public MeetRecordMessage getMeetRecordMessage() {
		return meetRecordMessage;
	}

	public void setMeetRecordMessage(MeetRecordMessage meetRecordMessage) {
		this.meetRecordMessage = meetRecordMessage;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getQuerySQL() {
		return querySQL;
	}

	public void setQuerySQL(String querySQL) {
		this.querySQL = querySQL;
	}
}
